package ioperations;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:张熳
 * Date:2025-09-04
 * Time:17:21
 */
//删除
public class DelOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书❌");
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入要删除的图书书名：");
        String name = scanner.nextLine();
        //找到这本书
        int pos = -1;
        int i = 0;
        int currentSize = bookList.getUsedSize();
        for (; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            if (book.getName().equals(name)) {
                System.out.print("找到这本书了！");
                pos = i;
                break;
            }
        }
        if (i == currentSize) {
            System.out.println("没有找到你要删除的图书！");
            return;
        }
        //到这里开始删除图书
        for (int j = pos; j < currentSize-1; j++) {
            //bookList[j] == bookList[j+1];
            Book book = bookList.getBook(j+1);
            bookList.setBook(j,book);
        }
        bookList.setBook(currentSize-1,null);
        bookList.setUsedSize(currentSize-1);
        System.out.println("删除成功！");
    }
}
